import os
import sys
import logging



# Uninstall Django 0.96.
for k in [k for k in sys.modules if k.startswith('django')]:
  del sys.modules[k]

# Add Django 1.0 archive to the path.


if os.name == 'nt':
  os.unlink = lambda: None

django_path = 'django.zip'
sys.path.insert(0, os.path.abspath(django_path))

#sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))


# Custom Django configuration.
import django

# AppEngine imports.
from google.appengine.ext.webapp import util

from django.conf import settings
settings._target = None
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'





# Import various parts of Django.

import django.core.handlers.wsgi
import django.core.signals
import django.db
import django.dispatch.dispatcher
import django.forms

# Work-around to avoid warning about django.newforms in djangoforms.
django.newforms = django.forms

def log_exception(*args, **kwds):
  """Django signal handler to log an exception."""
  cls, err = sys.exc_info()[:2]
  logging.exception('Exception in request: %s: %s', cls.__name__, err)



# Log all exceptions detected by Django.
django.core.signals.got_request_exception.connect(log_exception)

# Unregister Django's default rollback event handler.
django.core.signals.got_request_exception.disconnect(
    django.db._rollback_on_exception)


def main():
  logging.getLogger().setLevel(logging.DEBUG)
  # Re-add Django 1.0 archive to the path, if needed.
  if django_path not in sys.path:
    sys.path.insert(0, os.path.abspath(django_path))

  # Run Django via WSGI.
  application = django.core.handlers.wsgi.WSGIHandler()
  util.run_wsgi_app(application)

if __name__ == '__main__':
  main()